package com.rdc.jiajiadictionary.translate;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.rdc.jiajiadictionary.R;
import com.rdc.jiajiadictionary.bean.Word;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TranslateFragment#newInstance} factory method to
 * create an instance of this fragment.
 *
 */
public class TranslateFragment extends Fragment implements TranslateViewInterface,View.OnClickListener{

    private Context context;
    private String input;//进行搜索翻译时EditText的输入
    private Word word;
    private Activity mActivity;
    private View mView;//整个碎片的view

    private TranslatePresenter translatePresenter;
    private TextView wordNameTv;
    private TextView englishTv;
    private TextView americanTv;
    private TextView meansTv;
    private TextView exchangeTv;
    private ImageView backIv;
    private ImageView resetInputIv;
    private ImageView addIv;
    private TextView addTv;
    private LinearLayout addLl;
    private ImageView englishIv;
    private ImageView americanIv;
    private LinearLayout display;
    private TextView findNothing;


    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TranslateFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TranslateFragment newInstance(String param1, String param2) {
        TranslateFragment fragment = new TranslateFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }


    public TranslateFragment(){

    }

    /**
     * 创建对象时传入EditText的输入内容
     * @param input
     */
    public TranslateFragment(String input) {
        // Required empty public constructor
        this.input = input;
    }

    @Override
    public void onResume() {
        super.onResume();
        //判断网络，有才进行网络请求
        ConnectivityManager connectionManager = (ConnectivityManager)
                getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            translatePresenter.translate(input);
        } else {

        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        new TranslatePresenter(this,context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view;
        ConnectivityManager connectionManager = (ConnectivityManager)
                getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();

        //有网显示获取到的单词翻译的view，没网显示提醒view
        if (networkInfo != null && networkInfo.isAvailable()) {
            view = inflater.inflate(R.layout.translate_fragment, container, false);
            mView = view;
            view.setVisibility(View.INVISIBLE);
            wordNameTv = (TextView) view.findViewById(R.id.tv_word_name);
            englishTv = (TextView) view.findViewById(R.id.tv_ph_en);
            americanTv = (TextView) view.findViewById(R.id.tv_ph_am);
            meansTv = (TextView) view.findViewById(R.id.tv_means);
            exchangeTv = (TextView) view.findViewById(R.id.tv_exchange);
            englishIv = (ImageView) view.findViewById(R.id.iv_english);
            americanIv = (ImageView)view.findViewById(R.id.iv_american);
            addIv = (ImageView) view.findViewById(R.id.iv_add);
            addTv = (TextView) view.findViewById(R.id.tv_add);
            addLl = (LinearLayout) view.findViewById(R.id.ll_add);
            display = (LinearLayout) view.findViewById(R.id.ll_display);
            findNothing = (TextView) view.findViewById(R.id.tv_no);
            americanIv.setOnClickListener(this);
            englishIv.setOnClickListener(this);
            addLl.setOnClickListener(this);
        } else {
            view = inflater.inflate(R.layout.translate_fragment_withoutinternet, container, false);
        }

        return view;
    }

    /**
     * 提供更新ui的方法给presenter
     * @param word
     */
    public void setData(Word word){
        this.word = word;
        mActivity = (Activity) context;
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (word.getWord_name()!=null){
                    findNothing.setVisibility(View.GONE);
                    display.setVisibility(View.VISIBLE);
                    wordNameTv.setText(word.getWord_name());
                    englishTv.setText("英 /"+word.getPh_en()+"/ ");
                    americanTv.setText("美 /"+word.getPh_am()+"/ ");
                    meansTv.setText(word.getPart());
                    exchangeTv.setText(word.getExchange());
                    //判断是否已添加到生词本
                    if (word.isExist()){
                        addTv.setText("已添加");
                        addIv.setBackgroundResource(R.drawable.added);
                    } else {
                        addTv.setText("生词本");
                        addIv.setBackgroundResource(R.drawable.add);
                    }
                }else {
                    display.setVisibility(View.INVISIBLE);
                    findNothing.setVisibility(View.VISIBLE);
                }
                mView.setVisibility(View.VISIBLE);
            }
        });
    }

    public void toast(String text){
        Toast.makeText(getContext (), text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.ll_add:
                if (addTv.getText() == "生词本"){
                    addTv.setText("已添加");
                    addIv.setBackgroundResource(R.drawable.added);
                    translatePresenter.addWordIntoNotebook();
                } else {
                    addTv.setText("生词本");
                    addIv.setBackgroundResource(R.drawable.add);
                    translatePresenter.removeWordFromNotebook();
                }
                break;
            case R.id.iv_american:
                translatePresenter.play(word.getPh_am_mp3());
                break;
            case R.id.iv_english:
                translatePresenter.play(word.getPh_en_mp3());
                break;
        }
    }

    /**
     * 绑定presenter
     * @param presenter
     */
    @Override
    public void setPresenter(TranslatePresenter presenter) {
        this.translatePresenter = presenter;
    }
}